package cn.com.pansky.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import cn.com.pansky.payment.PayConf;

public class readXml {

	/**
	 * 响应报文解析
	 * 
	 * @param xml
	 * @param puk
	 * @return String
	 */
	
	public static void readStringXml(String xml, String signKey) {
		
		Document doc = null;
		String merId = StringUtils.EMPTY;
		String orderNumber = StringUtils.EMPTY;
		String qid = StringUtils.EMPTY;
		String orderAmount = StringUtils.EMPTY;
		String payAmount = StringUtils.EMPTY;
		String state = StringUtils.EMPTY;
		String orderCurrency = StringUtils.EMPTY;
		String orderTime = StringUtils.EMPTY;
		String payName = StringUtils.EMPTY;
		String payBank = StringUtils.EMPTY;
		String payNum = StringUtils.EMPTY;
		String sign = StringUtils.EMPTY;
		try {
			doc = DocumentHelper.parseText(StringUtils.trim(xml)); // 将字符串转为XML
			Element rootElt = doc.getRootElement(); // 获取根节点
			
			String respCode = rootElt.elementText("respCode");// 返回码
			if (respCode.equals("00")) {
				Iterator<?> iter = rootElt.elementIterator("queryorder"); // 获取根节点下的子节点document
				while (iter.hasNext()) {
					Element itemEle = (Element) iter.next();
					merId = itemEle.elementTextTrim("merId");
					orderNumber = itemEle.elementTextTrim("orderNumber");
					qid = itemEle.elementTextTrim("qid");
					orderAmount = itemEle.elementTextTrim("orderAmount");
					payAmount = itemEle.elementTextTrim("payAmount");
					state = itemEle.elementTextTrim("state");
					orderCurrency = itemEle.elementTextTrim("orderCurrency");
					orderTime = itemEle.elementTextTrim("orderTime");
					payName = itemEle.elementTextTrim("payName");
					payBank = itemEle.elementTextTrim("payBank");
					payNum = itemEle.elementTextTrim("payNum");
					sign = itemEle.elementTextTrim("sign");
					// 签名验证
					Map<String, String> paramMap = new HashMap<String, String>();
					paramMap.put("merId", merId);
					paramMap.put("orderNumber", orderNumber);
					paramMap.put("qid", qid);
					paramMap.put("orderAmount", orderAmount);
					paramMap.put("payAmount", payAmount);
					paramMap.put("state", state);
					paramMap.put("orderCurrency", orderCurrency);
					paramMap.put("orderTime", orderTime);
					paramMap.put("payName", payName);
					paramMap.put("payBank", payBank);
					paramMap.put("payNum", payNum);
					
					String[] paramKeys = new String[] { "merId", "orderNumber", "qid", "orderAmount", "payAmount", "state", "orderCurrency", "orderTime", "payName", "payBank", "payNum", };// 构造签名参数数组
					String calsign = PayUtils.signDictionary(paramMap, paramKeys, signKey,PayConf.CHARSET);// 生成加密签名串
					
					if (calsign.toUpperCase().equals(sign.toUpperCase())) {
						System.out.println("签名验证成功");
					} else {
						System.out.println("签名验证失败");
					}
				}
				
			} else if (respCode.equals("03")) {
				System.out.println("无此记录");
			} else if (respCode.equals("04")) {
				System.out.println("数据格式错误");
			} else if (respCode.equals("05")) {
				System.out.println("商户订单号,交易时间,原始流水号不能全部为空");
			} else if (respCode.equals("06")) {
				System.out.println("签名校验未通过");
			}
			
		} catch (DocumentException e) {
			System.out.println("返回的XML文件解析错误1!" + e.getMessage());
		} catch (Exception e) {
			System.out.println("返回的XML文件解析错误2!" + e.getMessage());
		}
	}
}
